import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:riki_push/enum/phone_platform.dart';
import 'package:riki_push/factory/riki_base_push.dart';
import 'package:riki_push/factory/riki_hw_push.dart';
import 'package:riki_push/factory/riki_meizu_push.dart';
import 'package:riki_push/factory/riki_oppo_push.dart';
import 'package:riki_push/factory/riki_vivo_push.dart';
import 'package:riki_push/factory/riki_xm_push.dart';
import 'package:riki_push/jpush/riki_jpush.dart';
import 'package:riki_base_flutter/utils/text_util.dart';
import 'entity/push_info_entity.dart';

///
///  riki push
///

class RikiPush {
  // 工厂模式
  factory RikiPush() => _getInstance();

  static RikiPush get instance => _getInstance();
  static RikiPush _instance;

  RikiPush._internal();

  static RikiPush _getInstance() {
    if (_instance == null) {
      _instance = RikiPush._internal();
    }
    return _instance;
  }

  String _jpushAppKey;
  String _jpushChannel;
  String _xmAppId;
  String _xmAppKey;
  String _oppoAppKey;
  String _oppoAppSecret;
  String _meizuAppId;
  String _meizuAppKey;
  PhonePlatform _phonePlatform;

  PhonePlatform get phonePlatform => _phonePlatform;

  String get jpushAppKey => _jpushAppKey;

  String get jpushChannel => _jpushChannel;

  String get xmAppId => _xmAppId;

  String get xmAppKey => _xmAppKey;

  String get oppoAppSecret => _oppoAppSecret;

  String get oppoAppKey => _oppoAppKey;

  String get meizuAppId => _meizuAppId;

  String get meizuAppKey => _meizuAppKey;
  bool _isInit = false;

  bool _ingBindFactory = false;

  bool get ingBindFactory => _ingBindFactory;

  set ingBindFactory(bool value) {
    _ingBindFactory = value;
  }

  static FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  static AndroidFlutterLocalNotificationsPlugin _androidFlutterLocalNotificationsPlugin = AndroidFlutterLocalNotificationsPlugin();

  /// 初始化推送相关的配置
  /// [jpushAppKey]   极光appKey
  /// [jpushChannel]  极光channel
  /// [xmAppId]       小米appId
  /// [xmAppKey]      小米appKey
  void init({
    @required String jpushAppKey,
    @required String jpushChannel,
    @required bool jpushProduction,
    @required bool jpushProductionDebug,
    @required String xmAppId,
    @required String xmAppKey,
    @required String oppoKey,
    @required String oppoSecret,
    @required String meizuAppId,
    @required String meizuAppKey,
  }) async {
    if (_isInit) return;
    this._jpushAppKey = jpushAppKey;
    this._jpushChannel = jpushChannel;
    this._xmAppId = xmAppId;
    this._xmAppKey = xmAppKey;
    this._oppoAppKey = oppoKey;
    this._oppoAppSecret = oppoSecret;
    this._meizuAppId = meizuAppId;
    this._meizuAppKey = meizuAppKey;
    _isInit = true;

    _getPhonePlatform().then((value) {
      if (value == PhonePlatform.xiaomi) {
        RikiXmPush.init(appId: this.xmAppId, appKey: this.xmAppKey);
      } else if (value == PhonePlatform.oppo) {
        RikiOppoPush.init(appKey: this._oppoAppKey, appSecret: this.oppoAppSecret);
      } else if (value == PhonePlatform.vivo) {
        RikiVivoPush.init();
      } else if (value == PhonePlatform.meizu) {
        // RikiMeizuPush.addListener(_listenerMz);
        // RikiMeizuPush.init(appId: this.meizuAppId, appKey: this.meizuAppKey);
      }
    });

    ///初始化极光配置信息
    RikiJpush.instance.setJpush(this.jpushAppKey, this.jpushChannel, jpushProduction, jpushProductionDebug);
  }

  /// 初始化本地推送
  void initLocalPushConfig({
    @required AndroidInitializationSettings androidInitializationSettings,
    @required IOSInitializationSettings iosInitializationSettings,
  }) async {
    // 初始化localNotifition
    var initializationSettings = InitializationSettings(android: androidInitializationSettings, iOS: iosInitializationSettings);
    await _flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

  /// 重置
  void reset() {
    _isInit = false;
  }

  /// 获取厂商参数
  Future<PhonePlatform> _getPhonePlatform() async {
    if (this.phonePlatform == null) {
      this._phonePlatform = await RikiBasePush.phonePlatform;
    }
    return phonePlatform;
  }

  /// 获取推送的唯一标识
  Future<PushInfoEntity> getPushInfo({@required String jpushAlias}) async {
    if (ingBindFactory) {
      return null;
    }
    ingBindFactory = true;
    var platform = await _getPhonePlatform();

    /// 先默认等800毫米
    await Future.delayed(Duration(milliseconds: 800));
    var pushInfoEntity = PushInfoEntity();
    if (platform == PhonePlatform.ios) {
      pushInfoEntity.pushServerType = PushInfoEntity.type_ios;
    } else if (platform == PhonePlatform.huawei) {
      String hwToken = await RikiHwPush.hwToken;
      if (TextUtil.isEmpty(hwToken)) {
        pushInfoEntity.pushServerType = PushInfoEntity.type_jpush;
      } else {
        pushInfoEntity.pushServerType = PushInfoEntity.type_android_hw;
        pushInfoEntity.equipmentCode = hwToken;
      }
    } else if (platform == PhonePlatform.xiaomi && (this.xmAppId != null && this.xmAppKey != null)) {
      await RikiXmPush.init(appId: this.xmAppId, appKey: this.xmAppKey);
      await Future.delayed(Duration(milliseconds: 800));
      String xmRegid = await RikiXmPush.xmRegId;
      if (TextUtil.isEmpty(xmRegid)) {
        pushInfoEntity.pushServerType = PushInfoEntity.type_jpush;
      } else {
        pushInfoEntity.pushServerType = PushInfoEntity.type_android_xm;
        pushInfoEntity.equipmentCode = xmRegid;
      }
    } else if (platform == PhonePlatform.oppo && (this.oppoAppKey != null && this.oppoAppSecret != null)) {
      String oppoRid = await RikiOppoPush.oppoRid;
      if (TextUtil.isEmpty(oppoRid)) {
        pushInfoEntity.pushServerType = PushInfoEntity.type_jpush;
      } else {
        pushInfoEntity.pushServerType = PushInfoEntity.type_android_oppo;
        pushInfoEntity.equipmentCode = oppoRid;
      }
    } else if (platform == PhonePlatform.vivo) {
      String vivoRid = await RikiVivoPush.vivoRid;
      if (TextUtil.isEmpty(vivoRid)) {
        pushInfoEntity.pushServerType = PushInfoEntity.type_jpush;
      } else {
        pushInfoEntity.pushServerType = PushInfoEntity.type_android_vivo;
        pushInfoEntity.equipmentCode = vivoRid;
      }
    } else if (platform == PhonePlatform.meizu) {
      String mzRid = await RikiMeizuPush.meizuRid;
      if (TextUtil.isEmpty(mzRid)) {
        pushInfoEntity.pushServerType = PushInfoEntity.type_jpush;
      } else {
        pushInfoEntity.pushServerType = PushInfoEntity.type_android_meizu;
        pushInfoEntity.equipmentCode = mzRid;
      }
    } else {
      pushInfoEntity.pushServerType = PushInfoEntity.type_jpush;
    }
    _ingBindFactory = false;
    return pushInfoEntity;
  }

  /// 反初始化push
  Future unInitPush() async {
    var platform = await _getPhonePlatform();
    switch (platform) {
      case PhonePlatform.huawei:
        await RikiHwPush.deleteHwToken;
        break;
      case PhonePlatform.xiaomi:
        await RikiXmPush.unInit(appId: this.xmAppKey, appKey: this.xmAppKey);
        break;
      case PhonePlatform.oppo:
        await RikiOppoPush.unInit();
        break;
      case PhonePlatform.vivo:
        await RikiVivoPush.unInit();
        break;
      case PhonePlatform.meizu:
        await RikiMeizuPush.unInit(appId: this.meizuAppId, appKey: this.meizuAppKey);
        break;
      default:
        break;
    }
    _ingBindFactory = false;
    await RikiJpush.instance.unInitJPush();
    return Future.value();
  }

  /// - - - - - - - - - - - - -  - - - - -  notifition - - - - - - - - - - - - - - - - - - - - -

  /// 清除特定通知
  void clearNotification(int id) {
    _flutterLocalNotificationsPlugin.cancel(id);
  }

  /// 清除全部通知
  void clearAllNotification() {
    _flutterLocalNotificationsPlugin.cancelAll();
  }

  /// 显示本地通知
  /// [id] 消息id
  /// [title] 标题
  /// [body] 内容
  /// [notificationDetails] 通知配置（渠道等）
  /// [payload] 有效点击
  void showNotification(int id, String title, String body, NotificationDetails notificationDetails, {String payload}) async {
    await _flutterLocalNotificationsPlugin.show(id, title, body, notificationDetails, payload: payload);
  }

  /// 获取通道信息
  Future<List<AndroidNotificationChannel>> getNotificationChannels(List<String> channelIds) async {
    return await RikiBasePush.getNotificationChannels(channelIds);
  }

  /// 创建android推送渠道
  /// [androidNotificationChannel] 渠道配置
  void createNotificationChannel(AndroidNotificationChannel androidNotificationChannel) {
    _androidFlutterLocalNotificationsPlugin.createNotificationChannel(androidNotificationChannel);
  }

  /// 删除android推送渠道
  /// [channelId] 渠道id
  void deleteNotificationChannel(String channelId) {
    _androidFlutterLocalNotificationsPlugin.deleteNotificationChannel(channelId);
  }
}
